import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/components/Layout.vue'
import { isLoggedIn } from '@/utils/auth'

export const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/auth/Login.vue'),
      meta: { 
        title: '登录',
        requiresGuest: true, // 只允许未登录用户访问
        hideInTab: true // 不在Tab导航中显示
      }
    },
    {
      path: '/',
      component: Layout,
      redirect: '/dashboard',
      meta: { requiresAuth: true },
      children: [
        {
          path: 'dashboard',
          name: 'Dashboard',
          component: () => import('@/views/Dashboard.vue'),
          meta: { title: 'dashboard', requiresAuth: true }
        },
        {
          path: 'news/list',
          name: 'NewsList',
          component: () => import('@/views/news/NewsList.vue'),
          meta: { title: 'newsList', requiresAuth: true }
        },
        {
          path: 'news/review',
          name: 'NewsReview',
          component: () => import('@/views/news/NewsReview.vue'),
          meta: { title: 'newsReview', requiresAuth: true }
        },
        {
          path: 'news/edit/:id?',
          name: 'NewsEdit',
          component: () => import('@/views/news/NewsEdit.vue'),
          meta: { 
            title: 'editNews', 
            requiresAuth: true,
            hideInTab: true // 不在Tab导航中显示
          }
        },
        {
          path: 'user/list',
          name: 'UserList',
          component: () => import('@/views/user/UserList.vue'),
          meta: { title: 'userList', requiresAuth: true }
        },
        {
          path: 'user/creator-review',
          name: 'CreatorReview',
          component: () => import('@/views/user/CreatorReview.vue'),
          meta: { title: 'creatorReview', requiresAuth: true }
        },
        {
          path: 'comment/list',
          name: 'CommentList',
          component: () => import('@/views/comment/CommentList.vue'),
          meta: { title: 'commentList', requiresAuth: true }
        },
        {
          path: 'report/list',
          name: 'ReportList',
          component: () => import('@/views/report/ReportList.vue'),
          meta: { title: 'reportList', requiresAuth: true }
        },
        {
          path: 'toolbox/clear-token',
          name: 'ClearToken',
          component: () => import('@/views/toolbox/ClearToken.vue'),
          meta: { title: 'clearToken', requiresAuth: true }
        },
        {
          path: 'toolbox/file-list',
          name: 'FileList',
          component: () => import('@/views/toolbox/FileList.vue'),
          meta: { title: 'fileList', requiresAuth: true }
        }
      ]
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'NotFound',
      redirect: '/'
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const loggedIn = isLoggedIn()
  
  // 检查需要认证的路由
  if (to.meta.requiresAuth && !loggedIn) {
    // 允许访问，让API请求处理401错误并弹出弹窗
    // 这样用户可以停留在页面上，通过弹窗引导登录
    next()
    return
  }
  
  // 检查只允许未登录用户访问的路由
  if (to.meta.requiresGuest && loggedIn) {
    // 已登录用户尝试访问登录页，重定向到首页
    next('/')
    return
  }
  
  next()
})
